package com.ruoyi.system.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.FhStudentsInformation;
import com.ruoyi.system.service.IFhStudentsInformationService;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Controller
@RequestMapping("/system/DataImport")
public class DataImport extends BaseController {

    private String prefix = "other/DataImport";

    @Autowired(required = false)
    private IFhStudentsInformationService fhStudentsInformationService;

    @RequiresPermissions("system:other:DataImport")
    @GetMapping()
    public String information() {
        return prefix + "/DataImport";
    }

    @PostMapping("/upload")
    public String uploadExcel(@RequestParam("file") MultipartFile file, Model model) {
        if (file.isEmpty()) {
            model.addAttribute("error", "请选择文件！");
            return prefix + "/DataImport";
        }

        try {
            List<FhStudentsInformation> fhStudentsInformationList = parseExcel(file);
            for (int i = 0; i < fhStudentsInformationList.size(); i++) {
                fhStudentsInformationService.insertFhStudentsInformation(fhStudentsInformationList.get(i));
            }
            model.addAttribute("message", "成功导入 " + fhStudentsInformationList.size() + " 条数据！");
        } catch (Exception e) {
            model.addAttribute("error", "导入失败：" + e.getMessage());
        }

        return prefix + "/DataImport";
    }

    // 解析 Excel 核心方法
    private List<FhStudentsInformation> parseExcel(MultipartFile file) throws IOException {
        List<FhStudentsInformation> users = new ArrayList<>();

        // 根据文件类型创建 Workbook
        Workbook workbook = WorkbookFactory.create(file.getInputStream());
        Sheet sheet = workbook.getSheetAt(0); // 获取第一个Sheet

        for (Row row : sheet) {
            if (row.getRowNum() == 0) continue; // 跳过表头

            FhStudentsInformation studentsInformation = new FhStudentsInformation();
            studentsInformation.setStuName(row.getCell(0).getStringCellValue());
            Long sex = null;
            String str = row.getCell(1).getStringCellValue();
            if(str != null && str != "") {
                if(str.equals("男")) {
                    sex = 0l;
                } else if (str.equals("女")) {
                    sex = 1l;
                }
            } else {
                sex = 2l;
            }
            studentsInformation.setStuSex(sex);
            studentsInformation.setStuIdCard(row.getCell(2).getStringCellValue());
            studentsInformation.setStuCollege(row.getCell(3).getStringCellValue());
            studentsInformation.setStuClass(row.getCell(4).getStringCellValue());
            studentsInformation.setStuAddress(row.getCell(5).getStringCellValue());
//            studentsInformation.setStuBirthday(row.getCell(6).getStringCellValue());
            studentsInformation.setStuPhoneNumber((long) row.getCell(7).getNumericCellValue());
            studentsInformation.setStuHomePhoneNumber((long) row.getCell(8).getNumericCellValue());
            studentsInformation.setStuInstructor(row.getCell(9).getStringCellValue());
            studentsInformation.setStuHeadTeacher(row.getCell(10).getStringCellValue());
            studentsInformation.setStuHeadTeacherPhoneNumber((long) row.getCell(11).getNumericCellValue());
            studentsInformation.setStuCreateTime(DateUtils.getTime());

            users.add(studentsInformation);
        }

        workbook.close();
        return users;
    }

}
